package com.alks.function.controller.biocontrol.deptwork;

import com.alibaba.excel.util.StringUtils;
import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.RoleCheck;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.data.dto.biocontrol.deptwork.CutSlipDto;
import com.alks.function.data.request.biocontrol.deptwork.CutInRequest;
import com.alks.function.data.request.biocontrol.deptwork.CutStkRequest;
import com.alks.function.service.biocontrol.deptwork.ICpCutService;
import com.alks.function.service.biocontrol.deptwork.ICutInService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@ApiRestController("/bioDeptWrok")
@Api(value = "生控作业", tags = "生控部-生控作业-冲刀入库作业")
@RequiredArgsConstructor
@RoleCheck(prgIds = "140120")
public class CutInController {
    private final ICutInService service;
    private final ICpCutService cpInfoService;
    private final String slipType = "I";

    @PutMapping("/getCutInHeadQuery")
    @ApiOperation(value = "冲刀入库单查询-可条件查询", tags = "生控部-生控作业-冲刀入库作业")
    public ResponseInfo getCutInHeadQuery(@RequestBody CutStkRequest request) {
        return service.getCutHeadQuery(request,slipType);
    }

    @PutMapping("/CutInHeadCheck")
    @ApiOperation(value = "冲刀入库单检核-更新", tags = "生控部-生控作业-冲刀入库作业")
    public ResponseInfo CutInHeadCheck(@RequestParam @ApiParam("订单号") String slipNo) {
        return service.cutHeadCheck(slipNo,slipType);
    }

    @PostMapping("/getCutInDetQuery")
    @ApiOperation(value = "冲刀入库单明细查询-条件查询", tags = "生控部-生控作业-冲刀入库作业")
    public ResponseInfo getCutInDetQuery(@RequestParam@ApiParam("订单号") String slipNo) {
        CutSlipDto dto = service.getCutDetQuery(slipNo, slipType);
        return ResponseInfo.ok(dto);
    }

    @PutMapping("/CutInAdd")
    @ApiOperation(value = "冲刀入库单创建-新增", tags = "生控部-生控作业-冲刀入库作业")
    public ResponseInfo cutInAdd(@RequestBody CutInRequest request) {
        /*冲刀号检测*/
        if (StringUtils.isBlank(request.getCutNo())) {
            throw new ServiceErrorException("冲刀号不能为空");
        }
        cpInfoService.cutNoCheck(request.getCutNo());
        String slipNo = service.cutAdd(request, slipType);
        return ResponseInfo.ok(slipNo);
    }

    @PutMapping("/CutInUp")
    @ApiOperation(value = "冲刀入库单更新-更新", tags = "生控部-生控作业-冲刀入库作业")
    public ResponseInfo cutInUp(@RequestBody CutInRequest request) {
        /*冲刀号检测*/
        if (StringUtils.isBlank(request.getSlipNo())){
            throw new ServiceErrorException("订单号不能为空");
        }if (StringUtils.isBlank(request.getCutNo())){
            throw new ServiceErrorException("冲刀号不能为空");
        }
        cpInfoService.cutNoCheck(request.getCutNo());
        String slipNo = service.cutUp(request, slipType);
        return ResponseInfo.ok(slipNo);
    }

    @PutMapping("/CutInDetPart")
    @ApiOperation(value = "取冲刀部位-更新", tags = "生控部-生控作业-冲刀入库作业")
    public ResponseInfo CutInDetPart(@RequestBody CutInRequest request) {
        /*冲刀号检测*/
        if (request.getSlipNo()==null){
            if (StringUtils.isNotBlank(request.getCutNo())){
                cpInfoService.cutNoCheck(request.getCutNo());
            }else {
                throw new ServiceErrorException("顶栏冲刀号不能为空");
            }
        }
        String slipNo = service.cutDetPart(request, slipType);
        return ResponseInfo.ok(slipNo);
    }

    @PostMapping("/CutInDelete")
    @ApiOperation(value = "整单删除-删除", tags = "生控部-生控作业-冲刀入库作业")
    public ResponseInfo CutInDelete(@RequestParam@ApiParam("订单号") String slipNo) {
        return service.cutDelete(slipNo,slipType);
    }

    @PutMapping("/CutInDetDelete")
    @ApiOperation(value = "冲刀入库明细删除-删除", tags = "生控部-生控作业-冲刀入库作业")
    public ResponseInfo CutInDetDelete(@RequestParam@ApiParam(value = "订单号",required = true) String slipNo,
                                        @RequestParam@ApiParam(value = "序号",required = true) String seqNo) {
        return service.cutDetDelete(slipNo,seqNo,slipType);
    }
}
